package com.tianqu.tms.core.webservice.weichat.api.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Stub;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.util.enums.ApiLogFuncType;
import com.tianqu.tms.core.webservice.weichat.api.rpc.carservice.CarService_PortType;
import com.tianqu.tms.core.webservice.weichat.api.rpc.carservice.CarService_ServiceLocator;
import com.tianqu.tms.core.webservice.weichat.api.rpc.transferserviceI.TransferServiceImpl;
import com.tianqu.tms.core.webservice.weichat.api.rpc.transferserviceI.TransferServiceImplServiceLocator;
import com.tianqu.tms.core.webservice.weichat.api.rpc.uploadservice.UploadService_PortType;
import com.tianqu.tms.core.webservice.weichat.api.rpc.uploadservice.UploadService_ServiceLocator;
import com.tianqu.tms.core.webservice.weichat.api.rpc.userservice.UserService_PortType;
import com.tianqu.tms.core.webservice.weichat.api.rpc.userservice.UserService_ServiceLocator;
import com.tianqu.tms.entity.TmsApicLogInfo;
import com.tianqu.tms.entity.TmsSmsInfo;

/**
 * @author Huang Wei
 *
 */
public abstract class AbsoluteManager {
	
	protected Logger LOGGER = Logger.getLogger(getClass());
	private static final String MESSAGE_SN = "SDK-DLS-010-00344";
	private static final String MESSAGE_PW = "911922";
	private static final int TIMEOUT = 10000;//ms
 
	
	/**
	 * @return
	 * @throws ServiceException
	 */
	protected TransferServiceImpl getTransferService() throws ServiceException {
		TransferServiceImpl port = new TransferServiceImplServiceLocator().getTransferServiceImplPort();
		Stub sub = (Stub)port;
		sub.setTimeout(TIMEOUT);
		sub._setProperty(org.apache.axis.client.Call.CONNECTION_TIMEOUT_PROPERTY, TIMEOUT);
		sub._setProperty(org.apache.axis.client.Call.FAULT_ON_NO_RESPONSE, true);
		return port;
	}
	
	/**
	 * @return
	 * @throws ServiceException
	 */
	protected CarService_PortType getCarService() throws ServiceException {
		CarService_PortType port = new CarService_ServiceLocator().getCarSoap();
		Stub sub = (Stub)port;
		sub.setTimeout(TIMEOUT);
		sub._setProperty(org.apache.axis.client.Call.CONNECTION_TIMEOUT_PROPERTY, TIMEOUT);
		sub._setProperty(org.apache.axis.client.Call.FAULT_ON_NO_RESPONSE, true);
		return port;
	}
	
	protected UserService_PortType getUserService() throws ServiceException {
		UserService_PortType port = new UserService_ServiceLocator().getUserServiceSoap();
		Stub sub = (Stub)port;
		sub.setTimeout(TIMEOUT);
		sub._setProperty(org.apache.axis.client.Call.CONNECTION_TIMEOUT_PROPERTY, TIMEOUT);
		sub._setProperty(org.apache.axis.client.Call.FAULT_ON_NO_RESPONSE, true);
		return port;
	}
	
	protected UploadService_PortType getUploadService() throws ServiceException {
		UploadService_PortType port = new UploadService_ServiceLocator().getUploadSoap();
		Stub sub = (Stub)port;
		sub.setTimeout(TIMEOUT);
		sub._setProperty(org.apache.axis.client.Call.CONNECTION_TIMEOUT_PROPERTY, TIMEOUT);
		sub._setProperty(org.apache.axis.client.Call.FAULT_ON_NO_RESPONSE, true);
		return port;
	}
	
	private String getErrorMobileMessage(String message) {
		String suffix = "";
		try {
			suffix = new String(Registry.getConfig(TmsConstants.MOBILE_PHONE_MESSAGE_SUFFIX_KEY).getBytes("ISO-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			suffix = "";
			LOGGER.error("getErrorMobileMessage error.",e);
		}
		return "[信息同步发送失败通知] "+DateUtil.DateToString12(new Date())+" "+message+" "+suffix;
	}
	
	protected void sendMobileMessage(String message){
		sendMobileMessage(false,Registry.getConfig(TmsConstants.MOBILE_PHONE_KEY),getErrorMobileMessage(message));
	}
	
	protected void sendMobileMessage(boolean isBatchCall,String message){
		sendMobileMessage(isBatchCall,Registry.getConfig(TmsConstants.MOBILE_PHONE_KEY),getErrorMobileMessage(message));
	}
	
	protected void sendMobileMessage(boolean isBatchCall,String phoneNumber,String message){
		if(StringUtil.isNullOrBlank(phoneNumber)) return;
		if(!"on".equalsIgnoreCase(Registry.getConfig(TmsConstants.MOBILE_PHONE_SWITCH_KEY))) return;
		try {
			if(!isBatchCall){
				TmsSmsInfo  sms = new TmsSmsInfo();
				sms.setSerial(String.valueOf(System.currentTimeMillis()));
				sms.setSmsContent(message);
				sms.setInsertUserId(TmsConstants.SYSTEM_OPERATOR_ID);
				sms.setOwnerPhone(phoneNumber);
				sms.setPepType(2);
				sms.setSendFrom(2);
				sms.insertSmsInfo(sms);
			}
			//TODO::SEND MAIL
//			Client client = new Client(MESSAGE_SN, MESSAGE_PW);
//			ArrayList<String> numbers = new ArrayList<String>();
//			Collections.addAll(numbers, phoneNumber.split(","));
//			client.mt(numbers,message, "", "", "");
		} catch (Exception e) {
			LOGGER.error("send message error.",e);
		}
	}
	
	protected void saveApiLog(ApiLogFuncType type,String content,String result) {
		try {
			//
			TxController controller = TxController.getTxController();
			controller.begin();
			TmsApicLogInfo log = new TmsApicLogInfo();
			log.setFuncType(type.getValue());
			log.setDataInfo(content);
			log.setReason(result);
			log.setUpdateUserId(TmsConstants.SYSTEM_OPERATOR_ID);
			log.insertApiLog(log);
			controller.commit();
		} catch (Exception e) {
			LOGGER.error("saveApiLog error.",e);
		}
	}
}
